package com.qtu.order_service.controller;




import com.qtu.entity.CartItem;
import com.qtu.entity.SettlementBean;
import com.qtu.order_service.service.CartService;

import com.qtu.order_service.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author CaiYinWei
 * @create 2020-07-07 17:38
 */
@Controller
@RequestMapping("api/car")
public class CarController {

    @Autowired
    private CartService cartService;


    @PostMapping("getCartItemMap")
    @ResponseBody
    public Map<String, Object> getCartItemMap(String token){
        Map<String,Object> param=new HashMap<>();
        Set<CartItem> cartItemList = cartService.getCartItemMap(token);
        param.put("items",cartItemList);
        return param;
    }

    @PostMapping("addToCart")
    @ResponseBody
    public HashMap<String, String> addToCart(String token, Integer goodesId, Integer num){
        HashMap<String, String> map = new HashMap<>();
        String s = cartService.addToCart(token, goodesId, num);
        if("ok".equals(s)){
            map.put("message","添加成功！");
            return map;
        }
        map.put("message","添加失败！");
        return map;
    }
    @GetMapping("deleteItem")
    @ResponseBody
    public String deleteItemFromCart(String token,Integer goodesId){
        String s = cartService.deleteItemFromCart(token, goodesId);
        return s;
    }

    @GetMapping("UpdateItem")
    @ResponseBody
    public String UpdateItemNumber(String token,Integer id,Integer num){
        String s = cartService.UpdateItemNumber(token, id,num);
        return s;

    }

    @PostMapping("settlement")
    @ResponseBody
    public Map<String, Object> settlement(String token,String itemIds){
        String[] split = itemIds.split(",");
        List<String> ids = Arrays.asList(split);
        SettlementBean settlement = cartService.settlement(token, ids);
        Map<String,Object> param=new HashMap<>();
        param.put("settlement",settlement);
        return param;
    }
    @PostMapping("creatOrder")
    @ResponseBody
    public ConcurrentHashMap<Object, Object> creatOrder(String token, String addressId, String remark, String settle){
        Set<CartItem> set = JsonUtils.jsonToSet(settle, CartItem.class);
        ConcurrentHashMap<Object, Object> map = cartService.commitOrder(token, set, remark, addressId);
        return map;
    }
    @PostMapping("pay")
    @ResponseBody
    public ConcurrentHashMap<Object, Object> pay(String token,String orderId, String payType, String money){
        ConcurrentHashMap<Object, Object> map = cartService.toPay(token, orderId, Integer.parseInt(payType), Integer.parseInt(money));
        return map;
    }

}
